package com.scalar.chiptrack.operations.dataentry.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.operations.dataentry.forms.OperationCodeEntryForm;
import com.scalar.chiptrack.operations.dataentry.dao.OperationCodeDAO;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.users.User;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.ArrayList;

/**
 * Created by Sudhakar.
 * User: Administrator
 * Date: Sep 30, 2005
 * Time: 4:58:55 PM

 */
public class OperationCodeEntryAction extends ChipTrackAction
{
    private static final String ACTION_NAME = "/operationCodeEntry.do";

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) throws Exception
    {


        HttpSession session = request.getSession();
        User user = ( User ) session.getAttribute( SessionKeys.USER_KEY );

        if ( user == null )
        {
            session.setAttribute( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME );
            return ( mapping.findForward( "login" ) );
        }

        HashMap errors = new LinkedHashMap();
        HashMap info = new LinkedHashMap();
        String action = request.getParameter( "action" );

        String actionForward = "success";

        OperationCodeEntryForm operationCodeEntryForm = ( OperationCodeEntryForm) form;

        if ( action == null )
        {
            action = "";
        }

        ArrayList processList = getProcessList();
        request.setAttribute("processList", processList );

        if ( action.equalsIgnoreCase( "save" ) )
        {

            String operationCode    = request.getParameter("operationCode").trim();
            String operationDesc    = request.getParameter("oprationCodeDesc").trim();
            String processType      = request.getParameter("process").trim();
            String process          = OperationCodeDAO.checkOperationCode( operationCode );
            if( process != null )
            {
                errors.put( " Operation Code "+operationCodeEntryForm.getOperationCode() + "  already exists in " , process );
                request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
            }
            else
            {
                try
                {
                	OperationCodeDAO.saveOperationCode( operationCode, operationDesc, processType );
                    actionForward = "saveSuccess";
                    info.put( "Data has been updated successfully", "" );
                    request.setAttribute( SessionKeys.INFO_MESSAGES, info );

                }
                catch( Exception e )
                {
                    e.printStackTrace();
                    errors.put( "Error storing the Operation Code information in to Database", null );
                    request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
                }
            }
         }
        return ( mapping.findForward( actionForward ) );
    }

    /**
     * Returns an arraylist of processes
     *
     * @return
     */
    public ArrayList getProcessList()
    {
        ArrayList categoryList = new ArrayList();
		categoryList.add( new org.apache.struts.util.LabelValueBean( "Assembly", "Assembly" ) );
        categoryList.add( new org.apache.struts.util.LabelValueBean( "Test", "Test" ) );
        return categoryList;
    }

}
